/**
 * 预加载脚本
 * 用于主进程和渲染进程互相通信
 */
const {contextBridge, ipcRenderer} = require('electron/renderer')


//process.versions 版本信息
// 将主进程的API暴露给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
    setTitle: (title) => ipcRenderer.send('set-title', title),
    selectFile: (opt) => ipcRenderer.invoke('dialog:selectFile', opt),
    openFile: (img) => ipcRenderer.send('open-file', img),

    onNavigate: (callback) => ipcRenderer.on('navigate', (event, route) => callback(route)),
    onWindowResize: (callback) => ipcRenderer.on('window-resize', (event, width, height) => callback(width, height)),

    getSources: (options) => ipcRenderer.invoke('get-sources', options),

    createChildWindow: (routePath, options = {}) => ipcRenderer.invoke('create-child-window', routePath, options),
    showChildWindow: (routePath) => ipcRenderer.invoke('show-child-window', routePath),
    hideChildWindow: (routePath) => ipcRenderer.invoke('hide-child-window', routePath),
    closeChildWindow: (routePath) => ipcRenderer.invoke('close-child-window', routePath),
    isChildWindowExists: (routePath) => ipcRenderer.invoke('is-child-window-exists', routePath),
    sendChildWindowMessage: (routePath, msg) => ipcRenderer.invoke('send-child-window-message', routePath, msg),

    onChildWindowMessage: (callback) => ipcRenderer.on('on-child-window-message', (event, msg) => callback(msg)),

    // 信息提示框（无返回值）
    info: (message) => ipcRenderer.invoke('dialog-info', message),
    // 警告框（返回用户选择）
    warning: (message) => ipcRenderer.invoke('dialog-warning', message),
    // 错误框（无返回值）
    error: (message) => ipcRenderer.invoke('dialog-error', message),

    // 信令服务器消息处理
    onSignalingMessage: (callback) => {
        ipcRenderer.on('signaling-message', (event, data) => callback(data))
    },
    onSignalingLogMessage: (callback) => {
        ipcRenderer.on('signaling-log-message', (event, data) => callback(data))
    },
    sendSignalingMessage: (message) => ipcRenderer.invoke('send-signaling-message', message),
    getSignalingState: () => ipcRenderer.invoke('get-signaling-state'),

    // store 操作
    store: {
        get: (key) => ipcRenderer.invoke('store-get', key),
        set: (key, value) => ipcRenderer.invoke('store-set', { key, value }),
        delete: (key) => ipcRenderer.invoke('store-delete', key),
        clear: () => ipcRenderer.invoke('store-clear'),
    },

})

document.addEventListener('keydown', (event) => {
    if (event.key === 'F5') {
        event.preventDefault(); // 阻止默认行为
        window.location.reload();
    }
})